package com.cskaoyan.service;

import com.cskaoyan.bean.Address;
import com.cskaoyan.bean.AddressExample;
import com.cskaoyan.bean.Region;
import com.cskaoyan.bean.address.WXAddress;
import com.cskaoyan.bean.address.WXAddressDetail;
import com.cskaoyan.bean.address.WXAddressSave;
import com.cskaoyan.mapper.AddressMapper;
import com.cskaoyan.mapper.RegionMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.xml.crypto.Data;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author: heliming
 * @date: 2021/6/7
 * @description:
 */
@Service
public class WXAddressServiceImpl implements WXAddressService {

    @Autowired
    AddressMapper addressMapper;
    @Autowired
    RegionMapper regionMapper;

    @Override
    public List<WXAddress> selectAddressList(Integer userId) {

        ArrayList<WXAddress> wxAddresses = new ArrayList<>();
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andDeletedEqualTo(false);


        criteria.andUserIdEqualTo(userId);
        List<Address> addresses = addressMapper.selectByExample(addressExample);
        for (Address address : addresses) {
            WXAddress wxAddress = new WXAddress();
            wxAddress.setDetailedAddress(address.getAddress());
            wxAddress.setId(address.getId());
            wxAddress.setIsDefault(address.getIsDefault());
            wxAddress.setMobile(address.getMobile());
            wxAddress.setName(address.getName());
            wxAddresses.add(wxAddress);
        }
        return wxAddresses;
    }

    @Override
    public WXAddressDetail selectAddressDetail(Integer id) {
        WXAddressDetail wxAddressDetail = new WXAddressDetail();
        Address address = addressMapper.selectByPrimaryKey(id);


        BeanUtils.copyProperties(address, wxAddressDetail);
        Region region1 = regionMapper.selectByPrimaryKey(address.getProvinceId());
        wxAddressDetail.setProvinceName(region1.getName());
        Region region2 = regionMapper.selectByPrimaryKey(address.getCityId());
        wxAddressDetail.setCityName(region2.getName());
        Region region3 = regionMapper.selectByPrimaryKey(address.getAreaId());
        wxAddressDetail.setAreaName(region3.getName());


        return wxAddressDetail;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertAddressSave(WXAddressSave wxAddressSave,Integer userId) {


            Address address = new Address();
            address.setAddress(wxAddressSave.getAddress());
            address.setAreaId(wxAddressSave.getAreaId());
            address.setCityId(wxAddressSave.getCityId());

            address.setUserId(userId);
            address.setIsDefault(wxAddressSave.getIsDefault());
            address.setMobile(wxAddressSave.getMobile());
            address.setName(wxAddressSave.getName());
            address.setProvinceId(wxAddressSave.getProvinceId());
            address.setAddTime(new Date());

            addressMapper.insertSelective(address);
//        }


    }

    /**
     * 假删除操作
     * @param wxId
     */
    @Override
    public void updateAddress(Integer wxId) {
        Address address = new Address();
        address.setId(wxId);
        address.setDeleted(true);

        addressMapper.updateByPrimaryKeySelective(address);
    }

    @Override
    public Integer updateAddressDefault(WXAddressSave wxAddressSave) {
        /*
         * todo:userid临时
         */
        Integer userId = 1;

        Address address = new Address();
        BeanUtils.copyProperties(wxAddressSave, address);

        addressMapper.updateByPrimaryKeySelective(address);

        return wxAddressSave.getId();
    }

    /**
     * 将所有的地址设置成非默认
     * @param wxAddressSave
     */
    @Override
    public void updateAddressDefaultFalse(WXAddressSave wxAddressSave) {

        Address address = new Address();
        AddressExample addressExample = new AddressExample();
        AddressExample.Criteria criteria = addressExample.createCriteria();
        criteria.andIsDefaultEqualTo(true);
        address.setIsDefault(false);
        address.setUpdateTime(new Date());

        addressMapper.updateByExampleSelective(address, addressExample);

    }

    /**
     * HJL
     * 购物车下单之前回显需要 地址数据，由于前端给的地址id会有问题时，返回默认的地址或任意地址
     *
     * @param addressId
     * @param userId
     * @return
     */
    @Override
    public Address getAddressOrDefault(Integer addressId, Integer userId) {
        Address address = addressMapper.selectByPrimaryKey(addressId);
        if (address != null) {//找到前端传的
            return address;
        }
        address = getDefaultAddress(userId);//前端的数据有问题，返回默认
        if (address != null) {
            return  address;
        }
        List<Address> addressList = getUserAddressList(userId);
        if (addressList.size() > 0) {//没有默认，返回第一个
            return addressList.get(0);
        }
        return null;
    }
    //获取用户所有地址
    private List<Address> getUserAddressList(Integer userId) {
        AddressExample example = new AddressExample();
        AddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId).andDeletedEqualTo(false);
        List<Address> addresses = addressMapper.selectByExample(example);
        return addresses;
    }

    //获取用户默认地址
    private Address getDefaultAddress(Integer userId) {
        Address defaultAddress = null;
        AddressExample example = new AddressExample();
        AddressExample.Criteria criteria = example.createCriteria();
        criteria.andUserIdEqualTo(userId).andIsDefaultEqualTo(true).andDeletedEqualTo(false);
        List<Address> addresses = addressMapper.selectByExample(example);
        if (addresses.size() > 0) {
            defaultAddress = addresses.get(0);
        }
        return defaultAddress;
    }


}
